package cn.wolfcode.trip.admin.web.filter;

import cn.wolfcode.trip.admin.web.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImageFliter implements Filter{
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //对请求图片名字有/upload的请求进行拦截
        HttpServletRequest req = (HttpServletRequest)request;
        String image = req.getRequestURI();

        File file = new File(UploadUtil.IMG_PATH,image);

        if(file.exists()){
            byte[] bytes = FileUtils.readFileToByteArray(file);
            response.getOutputStream().write(bytes);
        }else{

            chain.doFilter(request,response );
        }


    }

    public void destroy() {

    }
}
